package com.atu.atugra.service.impl;
 
import com.atu.atugra.constant.AppHttpCodeEnum;
import com.atu.atugra.tools.BeanCopyUtils;
import com.atu.atugra.tools.CommonResult;
import com.atu.atugra.vo.ProListVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atu.atugra.mapper.ProductsMapper;
import com.atu.atugra.entity.Products;
import com.atu.atugra.service.ProductsService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Products)表服务实现类
 *
 * @author makejava
 * @since 2025-02-07 12:03:45
 */
@Service("productsService")
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
    private ProductsMapper productsMapper;
    @Override
    public CommonResult getPage(Integer page, Integer size) {
        //用BeanCopyUtils将查询到的数据转换成VO类分页后返回
        Page<Products> productsPage = new Page<>(page, size);
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Products::getProductId, 1);

        this.page(productsPage,queryWrapper);
        List<ProListVO> resultList = productsPage.getRecords().stream()
                .map(products -> BeanCopyUtils.copyBean(products, ProListVO.class))
                .collect(Collectors.toList());
        Page<ProListVO> productVOPage = new Page<>(page, size);
        productVOPage.setRecords(resultList);
        productVOPage.setTotal(productsPage.getTotal());
        productVOPage.setSize(productsPage.getSize());
        productVOPage.setCurrent(productsPage.getCurrent());
        // 返回转换后的列表
        return CommonResult.okResult(productVOPage);

    }

    @Override
    public CommonResult insert(Products products) {
        this.save(products);
        return CommonResult.okResult("添加成功");
    }

    @Override
    public CommonResult updatePro(Products products) {
        products.setUpdatedAt(new Date());
        this.updateById(products);
        return CommonResult.okResult("更新成功");
    }
    @Override
    public CommonResult deletePro(Integer id) {
        return CommonResult.okResult(this.removeById(id));
    }

    @Override
    public List<ProListVO> searchProducts(String keyword, Long categoryId, Integer page, Integer pageSize) {
        //用BeanCopyUtils将查询到的数据转换成VO类分页后返回
        Page<Products> productsPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(keyword != null, Products::getProductName, keyword)
                .eq(categoryId != null, Products::getCategoryId, categoryId)
                .ne(Products::getProductId, 1);

        this.page(productsPage,queryWrapper);
        List<ProListVO> resultList = productsPage.getRecords().stream()
                .map(products -> BeanCopyUtils.copyBean(products, ProListVO.class))
                .collect(Collectors.toList());

        // 返回转换后的列表
        return resultList;
    }

    @Override
    public CommonResult getMPage(Integer page, Integer size,String keyword) {
        //用BeanCopyUtils将查询到的数据转换成VO类分页后返回
        Page<Products> productsPage = new Page<>(page, size);
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(keyword != null, Products::getProductName, keyword);
        this.page(productsPage,queryWrapper);
        List<Products> resultList = productsPage.getRecords();
        // 返回转换后的列表
        return CommonResult.okResult(resultList);
    }
}
